﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspiration.Web.Controllers
{
    public class UploadController : Controller
    {
        //
        // GET: /Upload/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(FormCollection formValues)
        {

            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase nounsListFile, HttpPostedFileBase adjectivesListFile)
        {
            // Process uploads here
            //ViewResult["Result"]
            bool successfulProcessing = true;
            
            if (nounsListFile != null)
            {
                string upload_dir = Server.MapPath("~/app_data/upload/");
                string upload_fileName = System.IO.Path.Combine(upload_dir, nounsListFile.FileName);
                nounsListFile.SaveAs(upload_fileName);

                // do processing here
                Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
                Inspiration.Web.Models.Noun newNoun = null;
                using (System.IO.StreamReader sr = new System.IO.StreamReader(upload_fileName))
                {
                    while (sr.Peek() > -1)
                    {
                        newNoun = new Models.Noun();
                        newNoun.Text = sr.ReadLine().Trim();
                        entitiesRepository.Nouns.AddObject(newNoun);
                    }
                }
                entitiesRepository.SaveChanges();
            }

            if (adjectivesListFile != null)
            {
                string upload_dir = Server.MapPath("~/app_data/upload/");
                string upload_fileName = System.IO.Path.Combine(upload_dir, adjectivesListFile.FileName);
                adjectivesListFile.SaveAs(upload_fileName);
                
                // do processing here
                Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
                Inspiration.Web.Models.Adjective newAdjective = null;
                using (System.IO.StreamReader sr = new System.IO.StreamReader(upload_fileName))
                {
                    while (sr.Peek() > -1)
                    {
                        newAdjective = new Models.Adjective();
                        newAdjective.Text = sr.ReadLine().Trim();
                        entitiesRepository.Adjectives.AddObject(newAdjective);
                    }
                }
                entitiesRepository.SaveChanges();

            }

            if (successfulProcessing)
                ViewData["Result"] = "successful";
            else
                ViewData["Result"] = "failed";
            return View();
        }

    }
}
